package com.jincheng.web;

import com.jincheng.util.Contanst;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

import java.util.Map;

/**
 * @author Jane
 * @date 2024-12-24 9:48
 *
 *  p:prefix="/WEB-INF/jsp/"
 */
@Controller

@RequestMapping("/api/user/")
public class UserCh05AnnoController {

    @RequestMapping({"/","/login","/login.html"})
    public String toLogin(Model model){ //"login.jsp";受保护了,不能直接访问,通过请求去访问,登录页面
        return "/login.jsp";
    }

//    @RequestMapping("/doLogin")
//    public String doLogin(HttpServletRequest request, HttpServletResponse response){
//        System.out.println("hello --anno ====spring mvc");
//        System.out.println("username: "+ request.getParameter("username"));
//        System.out.println("passsword: "+ request.getParameter("passsword"));
//
//        if ("admin".equals(request.getParameter("username"))){
//            if ("123456".equals(request.getParameter("passsword"))){
//                request.getSession().setAttribute("loginUser","admin");
//                return ("success.jsp");//request.getDis..("success.jsp")...forward()
//            }else{
//                request.setAttribute("msg","密码错误登录失败");
//            }
//        }else{
//            request.setAttribute("msg","用户不存在,登录失败");
//        }
//        return ("login.jsp");
//    }
//    @RequestMapping("/doLogin")
//    //public String doLogin(Model model, HttpServletRequest request, HttpServletResponse response){
//    public String doLogin(Map map, HttpServletRequest request, HttpServletResponse response){
//        System.out.println("hello --anno ====spring mvc");
//        System.out.println("username: "+ request.getParameter("username"));
//        System.out.println("passsword: "+ request.getParameter("passsword"));
//
//        if ("admin".equals(request.getParameter("username"))){
//            if ("123456".equals(request.getParameter("passsword"))){
//                request.getSession().setAttribute("loginUser","admin");
//                //redirect
//                //return ("forward:/api/user/toMain");//request.getDis..("success.jsp")...forward()
////                return (Contanst.FORWARD + "/api/user/toMain");
//
//                return UrlBasedViewResolver.FORWARD_URL_PREFIX + "/api/user/toMain";
//            }else{
//                //request.setAttribute("msg","密码错误登录失败");
//                //model.addAttribute("msg","密码错误,登录失败");
//                map.put("msg","map-密码错误,登录失败");
//            }
//        }else{
//            //request.setAttribute("msg","用户不存在,登录失败");
//           // model.addAttribute("msg","用户不存在,登录失败");  //SPRING MVC
//            map.put("msg","map-用户不存在,登录失败");
//        }
//        //return ("login.jsp");
//        return "login.jsp";
//    }

    //入参的几种处理方式
    @RequestMapping("/doLogin")
    //public String doLogin(Model model, HttpServletRequest request, HttpServletResponse response){
    public String doLogin(Model model, @RequestParam(value = "username", required = true) String username
                ,@RequestParam(value = "passsword", required = true) String passsword
                ,@RequestParam(value = "pageIndex",defaultValue = "1")     String pageIndex
    ){
        System.out.println("hello --anno ====spring mvc");
        //System.out.println("username: "+ request.getParameter("username"));
       // System.out.println("passsword: "+ request.getParameter("passsword"));

        if ("admin".equals(username)){
            if ("123456".equals(passsword)){
                //request.getSession().setAttribute("loginUser","admin");
                return UrlBasedViewResolver.FORWARD_URL_PREFIX + "/api/user/toMain";
            }else{
                model.addAttribute("msg","密码错误,登录失败");
            }
        }else{
            //request.setAttribute("msg","用户不存在,登录失败");
             model.addAttribute("msg","用户不存在,登录失败");  //SPRING MVC
        }
        //return ("login.jsp");
        return "login.jsp";
    }

    @RequestMapping("/toMain")
    public String toMain(Model model){
        System.out.println("toMain");
        //用户列表
        //角色列表
        return  "success.jsp";

    }


}
